package com.linlang.grasharepro.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.linlang.grasharepro.R;
import com.linlang.grasharepro.dao.UserDB;
import com.linlang.grasharepro.model.User;
import com.linlang.grasharepro.utils.validation.FireEye;
import com.linlang.grasharepro.utils.validation.FireEyeEnv;
import com.linlang.grasharepro.utils.validation.Form;
import com.linlang.grasharepro.utils.validation.StaticPattern;
import com.linlang.grasharepro.utils.validation.TextViewLoader;
import com.linlang.grasharepro.utils.validation.ValuePattern;

/**
 * Created by lixin on 2016-8-8.
 */
public class RegistActivity extends Activity {

    private UserDB userDB;
    private String userType = "在校生";
    private FireEye fireEye;//校验类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
    }

    private void initView() {
        View formView = findViewById(R.id.form);
        Form form = new Form(formView);

        fireEye = new FireEye(this);
        fireEye.add(form.byId(R.id.et_userName), StaticPattern.Required.setMessage("请输入用户名"));
        fireEye.add(form.byId(R.id.et_regPassword), StaticPattern.Required);
        fireEye.add(form.byId(R.id.et_rePassword), ValuePattern.Required.setMessage("请再输一次"),ValuePattern.EqualsTo.lazy(new TextViewLoader(form.byId(R.id.et_regPassword))));
        fireEye.add(form.byId(R.id.et_school), StaticPattern.Required);
        fireEye.add(form.byId(R.id.et_major), StaticPattern.Required);
        fireEye.add(form.byId(R.id.et_academicStarts), StaticPattern.Required);

        FireEyeEnv.setDebug(true);
        FireEyeEnv.setVerbose(true);
    }

    /**
     * 注册
     * @param view
     */
    public void regist(View view){
        fireEye.dump();
        if (!fireEye.test(true).passed) {
            return;
        }

        EditText et_userName = (EditText) findViewById(R.id.et_userName);
        EditText et_password = (EditText) findViewById(R.id.et_regPassword);
        EditText et_password2 = (EditText) findViewById(R.id.et_rePassword);
        EditText et_major = (EditText) findViewById(R.id.et_major);
        RadioGroup et_userType = (RadioGroup) findViewById(R.id.radioGroupbiye);
        EditText et_school = (EditText) findViewById(R.id.et_school);
        EditText et_academicStarts = (EditText) findViewById(R.id.et_academicStarts);

        String userName = et_userName.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        String password2 = et_password2.getText().toString().trim();
        String major = et_major.getText().toString().trim();
        String school = et_school.getText().toString().trim();
        String academicStarts = et_academicStarts.getText().toString().trim();
        et_userType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //获取变更后的选中项的ID
                                 int radioButtonId = group.getCheckedRadioButtonId();
                                //根据ID获取RadioButton的实例
                                 RadioButton rb = (RadioButton)RegistActivity.this.findViewById(radioButtonId);
                                //更新文本内容，以符合选中项
                userType = rb.getText().toString();

            }
        });

        User user = new User();
        user.setLoginName(userName);
        user.setLoginPass(password);
        user.setAcademicStarts(academicStarts);
        user.setMajor(major);
        user.setRole(userType);
        user.setSchool(school);

        userDB = new UserDB(this);
        userDB.open();

        int strid = userDB.insertUser(user);
        if(strid != 0){
            Toast.makeText(RegistActivity.this, "注册成功，请登陆", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(RegistActivity.this,
                    LoginActivity.class);
            startActivity(intent);
        }
    }
}
